package com.app.controller;

import com.app.bean.Users;
import com.app.bean.UsersExample;
import com.app.core.ReturnVoCommon;
import com.app.dao.UsersMapper;
import com.app.util.Const;
import com.app.util.StringUtilsEx;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 师晓峰
 * @version V1.0
 * @date 2023/9/20 18:34
 * @Description: 用户相关的操作
 */
@Controller
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UsersMapper usersMapper;

    /**
     * 登录
     *
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public ReturnVoCommon login(Users users, HttpServletRequest request) {
        ReturnVoCommon vo = new ReturnVoCommon();
        UsersExample example = new UsersExample();
        UsersExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(users.getName());
        criteria.andPasswordEqualTo(users.getPassword());
        criteria.andAuthEqualTo(users.getAuth());
        List<Users> usersList = usersMapper.selectByExample(example);
        if (usersList == null || usersList.size() == 0) {
            vo.setCode(-1);
            vo.setErrMsg("用户名后者密码错误");
            return vo;
        }
        if (users.getAuth().equals("注册用户")) {
            vo.setCode(1);
            request.getSession().setAttribute(Const.CURRENT_USER, usersList.get(0));
        } else {
            // 管理员用户
            vo.setCode(2);
            request.getSession().setAttribute(Const.ADMIN_USER, usersList.get(0));
        }
        return vo;
    }


    /**
     * 注册
     *
     * @return
     */
    @RequestMapping("/register")
    @ResponseBody
    public ReturnVoCommon register(Users users, HttpServletRequest request) {
        ReturnVoCommon vo = new ReturnVoCommon();
        UsersExample example = new UsersExample();
        UsersExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(users.getName());
        List<Users> usersList = usersMapper.selectByExample(example);
        // usersList 得到的是一个非空对象
        if (usersList.size() > 0) {
            vo.setCode(-1);
            vo.setErrMsg("用户已存在！！");
            return vo;
        }
        users.setAuth("注册用户");
        //  todo 后面可以校验一下
        usersMapper.insert(users);
        return vo;
    }

    /**
     * 管理员退出
     * @param request
     * @return
     */
    @RequestMapping("/logout")
    public ModelAndView logout(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();
        request.getSession().removeAttribute(Const.ADMIN_USER);
        mav.setViewName("front/login.jsp");
        return mav;
    }


    /**
     * 注册用户列表，带翻页功能
     * @param page  当前第几页
     * @param rows  每页大小
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(Integer page, Integer rows) {
        Map<String,Object> map = new HashMap<>();
        if (StringUtilsEx.isEmpty(page)) {
            page = 1;
        }
        if (StringUtilsEx.isEmpty(rows)) {
            rows = 5;
        }
        // 分页
        PageHelper.startPage(page,rows);
        List<Users> usersList = usersMapper.selectByExample(new UsersExample());
        map.put("rows",usersList);
        PageInfo<Users> pageInfo = new PageInfo<Users>(usersList);
        map.put("total",pageInfo.getTotal());
        return map;
    }


    /**
     * 删除
     *
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ReturnVoCommon delete(String id) {
        ReturnVoCommon vo = new ReturnVoCommon();
        try{
            usersMapper.deleteByPrimaryKey(Integer.parseInt(id));
        }catch (Exception e){
            vo.setCode(-1);
            vo.setErrMsg("删除失败！");
        }
        vo.setCode(0);
        return vo;
    }
}
